<?php
/***
平台管理员登录
***/
namespace app\dboard\controller;
use think\captcha;
use think\facade\Cookie;
use think\facade\Session;
use think\facade\Url;
use think\Db;
use app\api\logic\Base as apiBase;
class Tlogin extends Tbase
{
    public function initialize() {
        parent::initialize();
    }
    public function index(){
        
    }
    /***登录***/
    public function sign(){
        return view('login/tsign_in');
    }
    /**提交登录***/
    public function signIn(){
        if(!$this->request->isAjax()){
            $this->res_data['Msg']='请用正确方式登录！';
            return $this->print_result($this->res_data);
        }
        $user_name=trim($this->request->post('user_name',''));
        $password=trim($this->request->post('password',''));
        if(empty($user_name)){
            $this->res_data['Msg']='登录名不能为空！';
            return $this->print_result($this->res_data);
        }
        if(empty($password)){
            $this->res_data['Msg']='登录密码不能为空！';
            return $this->print_result($this->res_data);
        }
        $password=get_admin_pwd($password);
        $where=array();
        $where[]=['tel','=',$user_name];
        $where[]=['status','=',1];
        $where[]=['pwd','=',$password];
        $info=Db::name('teacher')->where($where)->find();
        if(!$info){
            $this->res_data['Msg']='账号不正确或被禁用';
            return $this->print_result($this->res_data);
        }
        $data=array();
        $data['id']=$info['id'];
        $data['login_time']=time();
        // $data['updatetime']=time();
        $data['login_ip']=$this->request->ip();
        $data['login_times']=$info['login_times']+1;
        $token_data['created_at'] = time();
        $token_data['expires_in'] = 7200;
        $token_data['ip'] = $data['login_ip'];
        $token_data['uid'] = $info['id'];
        $data['token'] = (new apiBase())->createUserToken($token_data);
        $sesson_admin=array();
        $sesson_admin['sid'] =$info['id'];
        $sesson_admin['AdminID']=$info['id'];
        $sesson_admin['AdminName']=$info['tel'];
        $sesson_admin['AdminToken']=$data['token'];
        Session::set("TAdminAccount",$sesson_admin);
        Db::name('teacher')->update($data,['id'=>$info['id']]);
        $this->res_data['Code']=0;
        $strCookie=$sesson_admin['AdminID']."\t".$sesson_admin['sid']."\t".$sesson_admin['AdminName']."\t".$sesson_admin['AdminToken'];
        $encode=uc_authcode($strCookie,'ENCODE','TZSyadmin');
        Cookie::set('TZSyadminauths',$encode,array('expire'=>3600*7));
        $this->res_data['Data']='/dboard/Teacheradmin/courseware_lists';
        return $this->print_result($this->res_data);
    }
    /**退出**/
    public function signOut(){
        Session::clear();
        Cookie::delete('TZSyadminauths');
        Cookie::clear();
        $isTrue = Session::has('TAdminAccount.AdminID');
        if(!$isTrue){
            return $this->redirect('dboard/tlogin/sign');
        }
        return $this->error('退出失败');
    }
}
